package com.sise.parkinglot.composition;

import com.sise.parkinglot.entity.IncomeLog;
import com.sise.parkinglot.entity.Park;
import com.sise.parkinglot.mapper.IncomeLogMapper;
import com.sise.parkinglot.mapper.ParkMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author ZJ
 */
@Component
public class TimeTask {

    @Resource
    private IncomeLogMapper incomeLogMapper;

    @Resource
    private ParkMapper parkMapper;

    @Resource
    private PublicService publicService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        List<Park> oneDay = parkMapper.getOneEndDay(1);
        if (oneDay.size() > 0) {
            AtomicReference<BigDecimal> money = publicService.getIncome(oneDay);
            Date pastDate = publicService.getPastDate(1);
            incomeLogMapper.insert(new IncomeLog().setIncome(BigDecimal.valueOf(money.get().intValue())).setRecordDate(pastDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()));
        }
    }
}
